package b.j.a;

import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.database.DataSetObserver;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;

/* compiled from: CursorAdapter */
public abstract class a extends BaseAdapter implements Filterable, b.j.a.b.a {
    public boolean a;
    public boolean b;
    public Cursor c;
    public Context d;
    public int e;
    public a f;
    public DataSetObserver g;
    public b h;

    /* compiled from: CursorAdapter */
    public class a extends ContentObserver {
        public a() {
            super(new Handler());
        }

        public boolean deliverSelfNotifications() {
            return true;
        }

        public void onChange(boolean z) {
            a aVar = a.this;
            if (aVar.b) {
                Cursor cursor = aVar.c;
                if (cursor != null && !cursor.isClosed()) {
                    aVar.a = aVar.c.requery();
                }
            }
        }
    }

    /* compiled from: CursorAdapter */
    public class b extends DataSetObserver {
        public void onChanged() {
            a aVar = a.this;
            aVar.a = true;
            aVar.notifyDataSetChanged();
        }

        public void onInvalidated() {
            a aVar = a.this;
            aVar.a = false;
            aVar.notifyDataSetInvalidated();
        }
    }

    public a(Context context, Cursor cursor, boolean z) {
        boolean z2 = true;
        int i = z ? 1 : 2;
        if ((i & 1) == 1) {
            i |= 2;
            this.b = true;
        } else {
            this.b = false;
        }
        if (cursor == null) {
            z2 = false;
        }
        this.c = cursor;
        this.a = z2;
        this.d = context;
        this.e = z2 ? cursor.getColumnIndexOrThrow("_id") : -1;
        if ((i & 2) == 2) {
            this.f = new a();
            this.g = new b();
        } else {
            this.f = null;
            this.g = null;
        }
        if (z2) {
            a aVar = this.f;
            if (aVar != null) {
                cursor.registerContentObserver(aVar);
            }
            DataSetObserver dataSetObserver = this.g;
            if (dataSetObserver != null) {
                cursor.registerDataSetObserver(dataSetObserver);
            }
        }
    }

    public abstract void a(View view, Context context, Cursor cursor);

    public void b(Cursor cursor) {
        Cursor cursor2 = this.c;
        if (cursor == cursor2) {
            cursor2 = null;
        } else {
            a aVar;
            DataSetObserver dataSetObserver;
            if (cursor2 != null) {
                aVar = this.f;
                if (aVar != null) {
                    cursor2.unregisterContentObserver(aVar);
                }
                dataSetObserver = this.g;
                if (dataSetObserver != null) {
                    cursor2.unregisterDataSetObserver(dataSetObserver);
                }
            }
            this.c = cursor;
            if (cursor != null) {
                aVar = this.f;
                if (aVar != null) {
                    cursor.registerContentObserver(aVar);
                }
                dataSetObserver = this.g;
                if (dataSetObserver != null) {
                    cursor.registerDataSetObserver(dataSetObserver);
                }
                this.e = cursor.getColumnIndexOrThrow("_id");
                this.a = true;
                notifyDataSetChanged();
            } else {
                this.e = -1;
                this.a = false;
                notifyDataSetInvalidated();
            }
        }
        if (cursor2 != null) {
            cursor2.close();
        }
    }

    public abstract CharSequence c(Cursor cursor);

    public abstract View d(Context context, Cursor cursor, ViewGroup viewGroup);

    public int getCount() {
        if (this.a) {
            Cursor cursor = this.c;
            if (cursor != null) {
                return cursor.getCount();
            }
        }
        return 0;
    }

    public View getDropDownView(int i, View view, ViewGroup viewGroup) {
        if (!this.a) {
            return null;
        }
        this.c.moveToPosition(i);
        if (view == null) {
            c cVar = (c) this;
            view = cVar.k.inflate(cVar.j, viewGroup, false);
        }
        a(view, this.d, this.c);
        return view;
    }

    public Filter getFilter() {
        if (this.h == null) {
            this.h = new b(this);
        }
        return this.h;
    }

    public Object getItem(int i) {
        if (this.a) {
            Cursor cursor = this.c;
            if (cursor != null) {
                cursor.moveToPosition(i);
                return this.c;
            }
        }
        return null;
    }

    public long getItemId(int i) {
        if (this.a) {
            Cursor cursor = this.c;
            if (cursor != null && cursor.moveToPosition(i)) {
                return this.c.getLong(this.e);
            }
        }
        return 0;
    }

    public View getView(int i, View view, ViewGroup viewGroup) {
        if (!this.a) {
            throw new IllegalStateException("this should only be called when the cursor is valid");
        } else if (this.c.moveToPosition(i)) {
            if (view == null) {
                view = d(this.d, this.c, viewGroup);
            }
            a(view, this.d, this.c);
            return view;
        } else {
            throw new IllegalStateException(c.b.a.a.a.A("couldn't move cursor to position ", i));
        }
    }
}
